{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f2e0da5d-b189-43dd-8273-4b700e2b1ecf",
   "metadata": {},
   "source": [
    "# NumPy4\n",
    "\n",
    "## 向量（1维数组）\n",
    "\n",
    "- 向量加法 对应位置分量相加，结果还是向量\n",
    "- 数乘向量 结果是向量\n",
    "- 数量积（点积dot product） 结果是标量，对应位置分量相乘再求和\n",
    "- 模（L2范数）\n",
    "- 叉积\n",
    "    - 二维向量的叉积是标量，写成二阶行列式形式计算得到\n",
    "    - 三维向量的叉积是向量，写成三阶行列式，第一行是单位向量i,j,k，第二行是第一个向量，第三行是第二个向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "04178166-83a2-4a32-800e-d93f9cca4b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "917661ea-e005-4f18-989c-debe43f2252e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7302967433402215\n",
      "0.9704311900788593\n"
     ]
    }
   ],
   "source": [
    "m1 = np.array([4, 5, 1])\n",
    "m2 = np.array([5, 1, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "712cd93f-0daa-4c3a-bcaf-2295cfb78aa9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(6.48074069840786), np.float64(7.14142842854285))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数量积\n",
    "np.dot(m1, m2)\n",
    "# 模\n",
    "np.linalg.norm(m1), np.linalg.norm(m2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7b8837c3-02b5-462a-8edd-2feccbff44b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6.48074069840786, 7.14142842854285)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "42**0.5, 51**0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "923c719a-95cc-40f5-bf69-b67f7d77feb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 24, -15, -21])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 叉积\n",
    "np.cross(m1, m2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "473718e0-2027-4d74-8ac8-8d6449071842",
   "metadata": {},
   "source": [
    "## 线性代数\n",
    "\n",
    "NumPy的linalg提供了线性代数的运算。\n",
    "\n",
    "\n",
    "| 函数          | 说明                                                         |\n",
    "| ------------- | ------------------------------------------------------------ |\n",
    "| `diag`        | 以一维数组的形式返回方阵的对角线元素或将一维数组转换为方阵（非对角元素元素为0） |\n",
    "| `matmul`      | 矩阵乘法运算                                                 |\n",
    "| `trace`       | 计算对角线元素的和                                           |\n",
    "| `norm`        | 求矩阵或向量的范数                                           |\n",
    "| `det`         | 计算行列式的值                                               |\n",
    "| `matrix_rank` | 计算矩阵的秩                                                 |\n",
    "| `eig`         | 计算矩阵的特征值（*eigenvalue*）和特征向量（*eigenvector*）  |\n",
    "| `inv`         | 计算非奇异矩阵（ $\\small{n}$ 阶方阵）的逆矩阵                |\n",
    "| `pinv`        | 计算矩阵的摩尔-彭若斯（*Moore-Penrose*）广义逆               |\n",
    "| `qr`          | QR分解（把矩阵分解成一个正交矩阵与一个上三角矩阵的积）       |\n",
    "| `svd`         | 计算奇异值分解（*singular value decomposition*）             |\n",
    "| `solve`       | 解线性方程组 $\\small{\\boldsymbol{Ax}=\\boldsymbol{b}}$，其中 $\\small{\\boldsymbol{A}}$ 是一个方阵 |\n",
    "| `lstsq`       | 计算 $\\small{\\boldsymbol{Ax}=\\boldsymbol{b}}$ 的最小二乘解   |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b72eaa63-cc64-43a3-9a73-566122e7b2e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 2.],\n",
       "        [3., 4.]]),\n",
       " array([[-2. ,  1. ],\n",
       "        [ 1.5, -0.5]]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m3 = np.array([[1., 2.], [3., 4.]])\n",
    "\n",
    "# 逆矩阵\n",
    "m4 = np.linalg.inv(m3)\n",
    "m3, m4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8e675f6f-fc22-4863-b372-f4e70decae3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 0.],\n",
       "        [0., 1.]]),\n",
       " array([[1., 0.],\n",
       "        [0., 1.]]))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵乘法\n",
    "np.round(np.linalg.matmul(m3, m4)), np.round(m3 @ m4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "365eb186-a805-4b69-9fc9-bb01d8d35c25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 3, 5],\n",
       "        [2, 4, 6],\n",
       "        [4, 7, 9]]),\n",
       " np.float64(2.0000000000000013))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算行列式的值\n",
    "m5 = np.array([[1, 3, 5], [2, 4, 6], [4, 7, 9]])\n",
    "m5, np.linalg.det(m5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6f40e980-b89f-443d-a059-663e78c0428f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(3)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵的秩\n",
    "np.linalg.matrix_rank(m5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b6cebd1-f75c-4d09-8739-c0743e6fc8dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
